package com.car.advertising.activity.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.car.advertising.R;
import com.car.advertising.entity.MeEntity;
import com.car.advertising.view.RiseNumberTextView;
import com.support.widget.interfacelistener.OnRecyclerViewItemClickListener;
import com.support.widget.interfacelistener.OnRecyclerViewItemLongClickListener;
import com.support.widget.layout.adapter.BaseRecyclerViewAdapter;
import com.support.widget.layout.holder.BaseViewHolder;

import java.util.List;

/**
 * 开发者：LuoYi
 * Time: 2017 16:19 2017/4/10 04
 */

public class MePageAdapter extends BaseRecyclerViewAdapter<MeEntity, BaseViewHolder> {

    private int ITEM_HEADER = 0;
    private int ITEM_CONTENT = 1;
    private MeClickListener meClickListener;

    public MePageAdapter(Context context, List<MeEntity> meEntity, MeClickListener meClickListener) {
        super(context, meEntity);
        this.meClickListener = meClickListener;
    }

    @Override
    protected void bindView(BaseViewHolder holder, int position, MeEntity meEntity) {
        if (holder instanceof MeHeadHolder) {
            if (meEntity.getAmountBean() != null) {
                MeHeadHolder mePageHolder = (MeHeadHolder) holder;
                mePageHolder.fragmentInfoTv.setText(meEntity.getAmountBean().getUserName());
                mePageHolder.totalAmountRntv.setText(meEntity.getAmountBean().getTotalAmount());
                mePageHolder.timeAmountRntv.setText(meEntity.getAmountBean().getLastTimeAmount());
                mePageHolder.cumulativeAmountRntv.setText(meEntity.getAmountBean().getCumulativeAmount());
            }
        } else if (holder instanceof MePageHolder) {
            if (!TextUtils.isEmpty(meEntity.getNavigtion())) {
                MePageHolder mePageHolder = (MePageHolder) holder;
                setTextLeftImg(mePageHolder.meContentTv, meEntity.getPositionItem());
                mePageHolder.meContentTv.setText(meEntity.getNavigtion());
                if (meEntity.isNewHintMsg()) {
                    mePageHolder.solidView.setVisibility(View.VISIBLE);
                } else {
                    mePageHolder.solidView.setVisibility(View.GONE);
                }
                mePageHolder.rightTv.setText(meEntity.getNewContent());
                if (position == 1 || position == 4) {
                    mePageHolder.meView.setVisibility(View.VISIBLE);
                } else {
                    mePageHolder.meView.setVisibility(View.GONE);
                }
            }
        }
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == ITEM_HEADER) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.fragment_head_layout, parent, false);
            return new MeHeadHolder(mContext, view, this);
        } else {
            View view1 = LayoutInflater.from(mContext).inflate(R.layout.fragment_me_item, parent, false);
            return new MePageHolder(mContext, view1, this, getClickListener(), getLongClickListener());
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return ITEM_HEADER;
        } else {
            return ITEM_CONTENT;
        }
    }

    class MeHeadHolder extends BaseViewHolder {

        public TextView infoTv;
        public RiseNumberTextView totalAmountRntv;
        public RiseNumberTextView timeAmountRntv;
        public RiseNumberTextView cumulativeAmountRntv;
        public LinearLayout timeAmountLl;
        public LinearLayout cumulativeAmountLl;
        public TextView withdrawalsIbtn;
        public TextView fragmentInfoTv;

        public MeHeadHolder(Context context, View rootView, BaseRecyclerViewAdapter mBaseRecyclerViewAdapter) {
            super(context, rootView, mBaseRecyclerViewAdapter);
            fragmentInfoTv = (TextView) rootView.findViewById(R.id.fragment_info_tv);
            infoTv = (TextView) rootView.findViewById(R.id.fragment_info_tv);
            totalAmountRntv = (RiseNumberTextView) rootView.findViewById(R.id.fragment_total_amount_rntv);
            timeAmountRntv = (RiseNumberTextView) rootView.findViewById(R.id.fragment_last_time_amount_rntv);
            cumulativeAmountRntv = (RiseNumberTextView) rootView.findViewById(R.id.fragment_cumulative_amount_rntv);
            timeAmountLl = (LinearLayout) rootView.findViewById(R.id.fragment_last_time_amount_ll);
            cumulativeAmountLl = (LinearLayout) rootView.findViewById(R.id.fragment_cumulative_amount_ll);
            withdrawalsIbtn = (TextView) rootView.findViewById(R.id.fragment_withdrawals_tv);

            infoTv.setOnClickListener(new HeadClick('i'));
            totalAmountRntv.setOnClickListener(new HeadClick('t'));
            timeAmountRntv.setOnClickListener(new HeadClick('l'));
            cumulativeAmountRntv.setOnClickListener(new HeadClick('c'));
            withdrawalsIbtn.setOnClickListener(new HeadClick('w'));
        }

        class HeadClick implements View.OnClickListener {

            char type;

            public HeadClick(char type) {
                this.type = type;
            }

            @Override
            public void onClick(View v) {
                meClickListener.onMeHeadCLick(type);
            }
        }
    }

    class MePageHolder extends BaseViewHolder implements OnRecyclerViewItemClickListener, OnRecyclerViewItemLongClickListener {

        public OnRecyclerViewItemClickListener mListener;
        public OnRecyclerViewItemLongClickListener mLongClickListener;
        public TextView meContentTv;
        public RelativeLayout itemRl;
        public View solidView;
        public TextView rightTv;
        public View meView;

        public MePageHolder(Context context, View rootView, BaseRecyclerViewAdapter mBaseRecyclerViewAdapter, OnRecyclerViewItemClickListener clickListener, OnRecyclerViewItemLongClickListener longClickListener) {
            super(context, rootView, mBaseRecyclerViewAdapter);
            meView = (View) rootView.findViewById(R.id.me_view);
            itemRl = (RelativeLayout) rootView.findViewById(R.id.me_item_rl);
            meContentTv = (TextView) rootView.findViewById(R.id.me_content_tv);
            solidView = (View) rootView.findViewById(R.id.me_solid_view);
            rightTv = (TextView) rootView.findViewById(R.id.me_right_tv);
            this.mListener = clickListener;
            this.mLongClickListener = longClickListener;
            rootView.setOnClickListener(this);
            rootView.setOnLongClickListener(this);
        }

        @Override
        public void onItemClick(ViewGroup parent, View view, int position) {
            if (mListener != null) {
                mListener.onItemClick(parent, view, position);
            }
        }

        @Override
        public void onItemLongClick(ViewGroup parent, View view, int position) {
            if (mLongClickListener != null) {
                mLongClickListener.onItemLongClick(parent, view, position);
            }
        }
    }

    private void setTextLeftImg(TextView textView, int iconId) {
        int imgId = R.mipmap.task_log_icon;
        switch (iconId) {
            case 1:
                imgId = R.mipmap.task_log_icon;
                break;
            case 2:
                imgId = R.mipmap.income_details_icon;
                break;
            case 3:
                imgId = R.mipmap.bank_card_icon;
                break;
            case 4:
                imgId = R.mipmap.coupon_icon;
                break;
            case 5:
                imgId = R.mipmap.activity_center_icon;
                break;
            default:
                break;
        }
        Drawable drawable = mContext.getResources().getDrawable(imgId);
        // 这一步必须要做,否则不会显示.
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        textView.setCompoundDrawables(drawable, null, null, null);
    }

    public interface MeClickListener {

        void onMeHeadCLick(char type);

    }

}
